package com.taoban.shopdata.core;

import java.io.StringReader;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taoban.shopdata.entity.PriceUpdatedCommitResult;
import com.taoban.shopdata.utils.ConfigUtils;
import com.yufei.dataretriver.DataRetrieverFactory;
import com.yufei.dataretriver.DataRetrieverFeatures;
import com.yufei.dataretriver.HttpDataRetriever;
import com.yufei.utils.CommonUtil;


public class PriceUpdatedTaskRequest {
	
		public static  String urlForGetItemInfo=null;
		private static Log mLog = LogFactory.getLog(PriceUpdatedTaskRequest.class);

	public static PriceUpdatedCommitResult requestBatchItemInfos() {
		// TODO Auto-generated method stub
		//从spider-server获取商品信息将商品信息转换成对象形式
		 DataRetrieverFeatures dataRetrieverFeatures = new DataRetrieverFeatures(false, "post");
		 dataRetrieverFeatures.setConnectionTimeout(1000*30);
		 
		 dataRetrieverFeatures.setRequestTimeout(Integer.parseInt(ConfigUtils.getVlaue("//taskRequestTimeOut")));
		 HttpDataRetriever dataRetriever = DataRetrieverFactory
					.createDataRetriever(dataRetrieverFeatures);
		
		PriceUpdatedCommitResult amazonItemResult=null;
		try{
		    String responseItemInfos=null;
		    try {
		    	urlForGetItemInfo= ConfigUtils.getUrlForGetItemInfo();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				mLog.info("read the config.xml Error for get urlForGetItemInfo!");
				return null;
			} 
			
			dataRetriever.setUrl(new URL(urlForGetItemInfo));
			dataRetriever.connect();
			responseItemInfos=dataRetriever.getHtmlContent();
			
			// TODO Auto-generated catch block
			String message=null;
		//如果没有任何准备好的任务直接抛出异常
		if(responseItemInfos.startsWith("no")){
			mLog.info(responseItemInfos);
			throw new RuntimeException(responseItemInfos);

		}
		
		amazonItemResult = (PriceUpdatedCommitResult) CommonUtil.getObjectFromXml(new StringReader(responseItemInfos), PriceUpdatedCommitResult.class);
		dataRetriever.disconnect();
		}catch(Exception e){
			mLog.error("request url:"+urlForGetItemInfo+" error,the error message is "+e.getMessage()+"");
			throw new RuntimeException(e);
		}

		return amazonItemResult;
	}

}
